using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Game;
using IQIGame.Onigao.GameAOT;
using System.Collections.Generic;
using UnityEngine;

namespace IQIGame.Onigao.GamePlay
{
    public class UIN_LevelMapMemontoPanel : UIBaseNode
    {
        #region Template Generate,don't modify

        protected class UIB_UIN_LevelMapMemontoPanel
        {
            #region ObjectBinding Generate

            public ExButton btnMemorandum { protected set; get; }
            public GameObject btnFunction { protected set; get; }

            public virtual void InitBinding(ObjectBinding __binding)
            {
                __binding.TryGetVariableValue<ExButton>("btnMemorandum", out var __tbv0);
                btnMemorandum = __tbv0;
                __binding.TryGetVariableValue<GameObject>("btnFunction", out var __tbv1);
                btnFunction = __tbv1;
            }

            #endregion ObjectBinding Generate
        }

        #endregion Template Generate,don't modify


        #region fields

        private bool _expand;

        private readonly List<GameObject> _btnList = new();

        private int _functionCnt;

        #endregion fields

        #region properties

        protected UIB_UIN_LevelMapMemontoPanel ui { get; set; }
        protected UIMsgDispatcher uiMsgDispatcher => _msgDispatcher as UIMsgDispatcher;

        #endregion properties

        protected override void BeforeInit()
        {
            ui = new UIB_UIN_LevelMapMemontoPanel();
            ui.InitBinding(csObjBind);
        }

        protected override void OnInit()
        {
            ui.btnMemorandum.AddClickEvent(OnBtnMemorandumClick);
            AddBtn(ui.btnFunction, 0);
        }

        protected override void OnShow()
        {
            //test code
            _functionCnt = 5;
            Refresh();
        }

        protected override void OnHide()
        {

        }

        protected override void OnDispose()
        {
        }

        #region methonds

        private void OnBtnMemorandumClick()
        {
            _expand = !_expand;
            Refresh();
        }

        private void OnBtnFunctionClick(int index)
        {
            //todo by xg: call function
            Debug.Log($"OnBtnFunctionClick:{index}");
        }

        private void Refresh()
        {
            for (var i = 0; i < _functionCnt; i++)
            {
                if (i == _btnList.Count)
                {
                    AddBtn(Object.Instantiate(ui.btnFunction.gameObject), i);
                }

                _btnList[i].SetActive(_expand);
            }

            for (var i = _functionCnt; i < _btnList.Count; i++)
            {
                _btnList[i].SetActive(false);
            }
        }

        private void AddBtn(GameObject go, int index)
        {
            var btn = go.GetComponent<ExButton>();
            btn.AddClickEvent(() => OnBtnFunctionClick(index));
            _btnList.Add(go);
        }

        #endregion methonds
    }
}